package com.wn.servlet.demo02;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yellow Docter
 * @date 2022 -06-29
 * @desc
 *
 *  javaweb的乱码
 *
 *  01 数据提交的乱码
 *
 *     get 请求不会出现乱码     把汉字 转成URL编码 不会出现乱码 (浏览器.服务器就会自动解析)
 *
 *     post 提交数据 会出现中文乱码  请求体的数据,req默认使用 ios-8859-1 来获取请求体数据
 *     解决: request 设置 指定的解码字符集
 *     request.setCharacterEncoding("utf-8");
 *
 *  02 数据回写的乱码
 *
 *    乱码原因: 浏览器默认编码和 javaweb程序编码不一致
 *
 *    通过设置头信息告诉 浏览器用指定的编码来解析
 *    response.setContentType("text/html;charset=utf-8");
 */
@WebServlet("/code")
public class CodeServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        request.setCharacterEncoding("utf-8");

        String username = request.getParameter("username");
        System.out.println("username = " + username);

        response.setContentType("text/html;charset=utf-8");

        response.getWriter().write("中了500W美元");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);

    }
}
